// /**
//  * @param {number[][]} matrix
//  * @param {number} target
//  * @return {boolean}
//  */
//  var searchMatrix = function(matrix, target) {
//     let arr = matrix.flat(1)
//     //console.log(arr);
//     let l = 0,r = arr.length-1
//     while(r>=l){
//         let mid = (l+r)>>1
//         if(arr[mid]===target){
//             return true
//         }else if(arr[mid]<target){
//             l = mid +1
//         }else{
//             r = mid -1
//         }
//     }
//     return false
// };


/**
 * @param {number[][]} matrix
 * @param {number} target
 * @return {boolean}
 */
 var searchMatrix = function(matrix, target) {
    let x = matrix.length , y = matrix[0].length, xLeft = 0, xRight = x - 1
    while(xRight>=xLeft){
        let xMid = (xLeft + xRight)>>1
        //console.log(xMid);
        if(matrix[xMid][0]<=target&&target<=matrix[xMid][y-1]){
           return  binarySearch(matrix[xMid],target)
        }else if(target<matrix[xMid][0]){
            xRight = xMid - 1
        }else{
            xLeft = xMid + 1
        }
    }
    return false
};

function binarySearch(nums,target){
    let l =0 , r = nums.length-1
    while(r>=l){
        let mid = (l + r)>>1
        if(nums[mid]===target){
            return true
        }else if(nums[mid]>target){
            r = mid - 1
        }else{
            l = mid + 1
        }
    }
    return false
}
console.log(searchMatrix([[2,4,7],[9,10,17],[20,21,22]],20));
